package luke4fun.wheresmymoney.fragments;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Locale;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.impl.MonthAccAdapter;
import luke4fun.wheresmymoney.common.CommonConstants;
import luke4fun.wheresmymoney.db.AccDBHelper;
import luke4fun.wheresmymoney.db.DBConstants;
import luke4fun.wheresmymoney.service.async.MonthAsyncAccTask;
import luke4fun.wheresmymoney.util.FormatUtil;

public class MonthAccRecFragment extends Fragment implements OnScrollListener {

	private ExpandableListView accountingList;
	private MonthAccAdapter adapter;
	private Context context;
	private AccDBHelper dbHelper;
	private SQLiteDatabase db;

	private TextView monthText;
	private TextView yearText;
	private TextView monthSum;

	private long dateInMillis;
	
	public static MonthAccRecFragment getInstance(long dateInMillis) {
		MonthAccRecFragment fragment = new MonthAccRecFragment();
		Bundle bundle = new Bundle();
		bundle.putLong(CommonConstants.ARG_DATE, dateInMillis);
		fragment.setArguments(bundle);
		
		return fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		setHasOptionsMenu(true);

		View v = inflater.inflate(R.layout.fragment_month_history, container, false);
		initView(v);

		if (adapter == null) {
			adapter = new MonthAccAdapter(context);
		}
		accountingList.setAdapter(adapter);

		return v;
	}
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		dbHelper = new AccDBHelper(context);
//		if (savedInstanceState != null) {
//			this.dateInMillis = savedInstanceState.getLong("dateInMillis");
//		} else {
			this.dateInMillis = getArguments().getLong(CommonConstants.ARG_DATE);
//		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		refresh();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (dbHelper != null) {
			dbHelper.close();
		}
	}

	private void initView(View v) {
		context = getActivity();
		accountingList = (ExpandableListView) v.findViewById(R.id.month_acc_list);
		monthText = (TextView) v.findViewById(R.id.month_text);
		yearText = (TextView) v.findViewById(R.id.year_text);
		monthSum = (TextView) v.findViewById(R.id.month_sum);
	}

	private void setView() {
		db = dbHelper.getReadableDatabase();

		Calendar c1 = Calendar.getInstance();
		c1.clear();
		c1.setTimeInMillis(this.dateInMillis);
		
		int year = c1.get(Calendar.YEAR);
		int month = c1.get(Calendar.MONTH);
		
		yearText.setText("" + c1.get(Calendar.YEAR));
		monthText.setText(c1.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault()));
		c1.clear();
		c1.set(year, month, 1);
		
		Calendar c2 = Calendar.getInstance();
		c2.clear();
		c2.set(year, month+1, 1);

		Cursor cursor = db.rawQuery("SELECT SUM(price) as price FROM " + DBConstants.V_RECORD + " WHERE datatime >= ? AND datatime < ? AND pid = -1",
				new String[] { c1.getTimeInMillis() + "", c2.getTimeInMillis() + "" });
		
		while (cursor.moveToNext()) {
			monthSum.setText("$ "
					+ FormatUtil.formatNumber(cursor.getInt( cursor.getColumnIndex(DBConstants.RECORD_PRICE) )));
		}
		
		db.close();
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		refresh();
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}

	public void refresh() {
		if (context != null) {
			setView();
			new MonthAsyncAccTask(context, adapter, this.dateInMillis).execute();
		}
	}
}
